package com.ruoyi.law.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.ruoyi.common.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信跑龙套
 *
 * @author lie
 * @date 2022/11/30
 */
@Slf4j
public class SmsUtil {

    /**
     * 阿里云短信配置
     */
    private static final SmsProperties PROPERTIES = SpringUtil.getBean(SmsProperties.class);

    public static void send(String tel, String code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(PROPERTIES.getEndpoint(), PROPERTIES.getKeyid(), PROPERTIES.getKeysercret());
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(PROPERTIES.getSignName());
        request.setTemplateCode(PROPERTIES.getTemplateCode());
        // 设置手机号
        request.setPhoneNumbers(tel);
        Map<String, String> param = new HashMap<>(2);
        param.put("code", code);
        Gson gson = new Gson();
        String json = gson.toJson(param);
        // 设置随机验证码
        request.setTemplateParam(json);
        // 发送过程
        SendSmsResponse response = client.getAcsResponse(request);
        log.info(response.getMessage());
    }

    /**
     * 推荐人通过审核
     * （【律创法务】您推荐的{name}提交的订单已通过审核，请登录小程序查看推广业绩，感谢您对律创法务的支持！）
     *
     * @param tel  电话
     * @param name 名字
     * @throws ClientException 客户端异常
     */
    public static void send1(String tel, String name) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(PROPERTIES.getEndpoint(), PROPERTIES.getKeyid(), PROPERTIES.getKeysercret());
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(PROPERTIES.getSignName());
        request.setTemplateCode(PROPERTIES.getTemplateCode1());
        // 设置手机号
        request.setPhoneNumbers(tel);
        Map<String, String> param = new HashMap<>(2);
        param.put("name", name);
        Gson gson = new Gson();
        String json = gson.toJson(param);
        // 设置随机验证码
        request.setTemplateParam(json);
        // 发送过程
        SendSmsResponse response = client.getAcsResponse(request);
        log.info(response.getMessage());
    }

    /**
     * 提交的法律咨询订单已分配律师 给用户发短信
     * 【律创法务】您提交的法律咨询订单已分配律师，请耐心等待律师来电，或直接拨打律师电话15187020919进行咨询。
     *
     * @param tel   电话
     * @param phone 律师电话
     * @throws ClientException 客户端异常
     */
    public static void send2(String tel, String phone) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(PROPERTIES.getEndpoint(), PROPERTIES.getKeyid(), PROPERTIES.getKeysercret());
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(PROPERTIES.getSignName());
        request.setTemplateCode(PROPERTIES.getTemplateCode2());
        // 设置手机号
        request.setPhoneNumbers(tel);
        Map<String, String> param = new HashMap<>(2);
        param.put("phone", phone);
        Gson gson = new Gson();
        String json = gson.toJson(param);
        // 设置随机验证码
        request.setTemplateParam(json);
        // 发送过程
        SendSmsResponse response = client.getAcsResponse(request);
        log.info(response.getMessage());
    }

    /**
     * 法律服务订单分配承办人后 给承办人发短信
     * 【律创法务】225852提交的225852订单分配您为承办人，请及时登录小程序办理案件，谢谢！
     *
     * @param tel  电话
     * @param name 订单下单人姓名
     * @param type 订单类型
     * @throws ClientException 客户端异常
     */
    public static void send3(String tel, String name, String type) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(PROPERTIES.getEndpoint(), PROPERTIES.getKeyid(), PROPERTIES.getKeysercret());
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(PROPERTIES.getSignName());
        request.setTemplateCode(PROPERTIES.getTemplateCode3());
        // 设置手机号
        request.setPhoneNumbers(tel);
        Map<String, String> param = new HashMap<>(2);
        param.put("name", name);
        param.put("type", type);
        Gson gson = new Gson();
        String json = gson.toJson(param);
        // 设置随机验证码
        request.setTemplateParam(json);
        // 发送过程
        SendSmsResponse response = client.getAcsResponse(request);
        log.info(response.getMessage());
    }

    /**
     * 律师接到法律咨询订单 给律师发短信
     * （【律创法务】526586向您提交了关于526586的法律咨询订单，请拨打电话526586为其提供法律咨询，谢谢！）（和2是一起的）
     *
     * @param tel   电话
     * @param name  用户
     * @param type  订单类型
     * @param phone 用户电话
     * @throws ClientException 客户端异常
     */
    public static void send4(String tel, String name, String type, String phone) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(PROPERTIES.getEndpoint(), PROPERTIES.getKeyid(), PROPERTIES.getKeysercret());
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(PROPERTIES.getSignName());
        request.setTemplateCode(PROPERTIES.getTemplateCode4());
        // 设置手机号
        request.setPhoneNumbers(tel);
        Map<String, String> param = new HashMap<>(2);
        param.put("name", name);
        param.put("type", type);
        param.put("phone", phone);
        Gson gson = new Gson();
        String json = gson.toJson(param);
        // 设置随机验证码
        request.setTemplateParam(json);
        // 发送过程
        SendSmsResponse response = client.getAcsResponse(request);
        log.info(response.getMessage());
    }

    /**
     * 法律服务订单分配承办人后 给用户发短信
     * 【律创法务】您提交的法律服务订单已分配承办律师，您可以登陆小程序查看办理进度或者补充相关资料。
     *
     * @param tel 电话
     * @throws ClientException 客户端异常
     */
    public static void send5(String tel) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile(PROPERTIES.getEndpoint(), PROPERTIES.getKeyid(), PROPERTIES.getKeysercret());
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(PROPERTIES.getSignName());
        request.setTemplateCode(PROPERTIES.getTemplateCode5());
        // 设置手机号
        request.setPhoneNumbers(tel);
        // 发送过程
        SendSmsResponse response = client.getAcsResponse(request);
        log.info(response.getMessage());
    }
}
